import 'package:flutter/material.dart';
import 'package:shenling_logistics/pages/views/WaitView.dart';

class TaskIndex extends StatefulWidget {
  const TaskIndex({super.key});

  @override
  State<TaskIndex> createState() => _TaskIndexState();
}

class _TaskIndexState extends State<TaskIndex>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  // 标签数据
  List labels = ["待提货", "在途", "已完成"];
  List<Widget> tabs = [WaitView(), Text("在途"), Text("已完成")];
  // 顶部高亮索引
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
    // 监听 TabController 的动画变化
    _tabController.animation!.addListener(_tabChange);
  }

  @override
  void dispose() {
    // 释放资源
    _tabController.animation!.removeListener(_tabChange);
    _tabController.dispose();
    super.dispose();
  }

  void _tabChange() {
    setState(() {
      if (_currentIndex != _tabController.animation!.value.round()) {
        // print(_tabController.animation!.value.round());
        _currentIndex = _tabController.animation!.value.round();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        title: TabBar(
          indicatorWeight: 4,
          // 默认选中颜色
          // labelColor: Colors.black87,
          // // 未选中颜色
          // unselectedLabelColor: Colors.grey,

          // 分割线高度
          dividerHeight: 0,
          // 指示器颜色
          indicatorColor: Colors.redAccent[400],
          // 默认选中字体样式
          labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
          // Tab 控制器
          controller: _tabController,
          tabs: labels
              .map((e) => Tab(
                      child: Text(
                    e,
                    style: TextStyle(
                        color: labels[_currentIndex] == e
                            ? Colors.black87
                            : Colors.grey),
                  )))
              .toList(),
        ),
      ),
      body: TabBarView(
        //构建
        controller: _tabController,
        children: tabs.map((e) {
          return Center(
            child: Container(
              alignment: Alignment.center,
              child: e,
            ),
          );
        }).toList(),
      ),
    );
  }
}
